#!/usr/bin/python
#-*-coding: utf-8-*-

import os
import sys
import random
import string

def getRandomQuote():
    files=os.listdir("/usr/share/games/fortune")
    files=[f for f in files if ".dat" not in f and ".u8" not in f]
    f=random.sample(files, 1)
    s=file("/usr/share/games/fortune/"+f[0]).read()
    quotes=s.split("\n%\n") 
    return (random.sample(quotes, 1))[0]

def getQuote(pat):
    files=os.listdir("/usr/share/games/fortune")
    files=[f for f in files if ".dat" not in f and ".u8" not in f]
    random.shuffle(files)
    for f in files:
        s=file("/usr/share/games/fortune/"+f).read()
        quotes=s.split("\n%\n")
        for q in quotes:
            if pat in q:
                return q
            
    return None                

if __name__=="__main__":
    if len(sys.argv)>1 and sys.argv[1]=="-m":
        quote=getQuote(sys.argv[2])
    else:
        quote=getRandomQuote()
    print quote
            
            
        
        



